package com.yjd.pub.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.model.PagerModel;
import com.yjd.comm.base.service.IPager;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.system.dto.OperParamDto;
import com.yjd.comm.system.model.LinkageModel;
import com.yjd.comm.system.service.ILinkageService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.PagerUtil;
import com.yjd.pub.system.dao.ILinkageDao;

/**
 * <b>description</b>：数据字典业务实现<br>
 * <b>time</b>：2014-11-08 14:31:38 <br>
 * <b>author</b>：  ready likun_557@163.com
 */
@Service("linkageService")
public class LinkageServiceImpl extends BaseServiceImpl implements ILinkageService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public LinkageModel insert(LinkageModel model) throws Exception {
		return this.linkageDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(LinkageModel model) throws Exception {
		return this.linkageDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param linkage_id
	 * @return
	 * @throws Exception
	 */
	public int delete(int linkage_id,OperParamDto operParamDto) throws Exception {
		LinkageModel linkageModel= this.linkageDao.getModelById(linkage_id, true);
		this.adminLog(operParamDto, "删除字典类型[ID:" + 	linkage_id+"名字："+linkageModel.getName()+"Code:"+linkageModel.getCode()+ "]", FrameUtil.getTime(null));
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("linkage_id", linkage_id);
		return this.linkageDao.deleteModel(paramMap, true);
	}

	@Resource
	private ILinkageDao linkageDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.linkageDao;
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public PagerModel getMyPageModel(Map paramMap) throws Exception{
		return PagerUtil.getPagerModel(new IPager() {
			@Override
			public List getPageList(Map paramMap) throws Exception {
				return linkageDao.getMyPageModel(paramMap);
			}

			@Override
			public long getPageCount(Map paramMap) throws Exception {
				return linkageDao.getMyPageModelCount(paramMap);
			}
		}, paramMap, this.getBaseDao().getDbtype());
	}

	@Override
	public void insertByCreate(String code, String name,OperParamDto operParamDto) throws Exception {
		LinkageModel model =new LinkageModel();
		model.setCode("parent_"+code);
		model.setName(name);
		model.setLinkage_id(null);
		LinkageModel myModel =this.insert(model);
		LinkageModel childModel =new LinkageModel();
		childModel.setCode(code);
		childModel.setName("example");
		childModel.setParentid(myModel.getLinkage_id());
		childModel.setLinkage_id(null);
		this.insert(childModel );
		this.adminLog(operParamDto, "增加字典类型[ID:" + 	myModel.getLinkage_id()+"名字："+myModel.getName()+"Code:"+myModel.getCode()+ "]", FrameUtil.getTime(null));
	}

	@Override
	public void updateByAll(LinkageModel parentLlinkageModel,
			List<LinkageModel> childList,OperParamDto operParamDto) throws Exception {
		String ids="";
		for (LinkageModel linkageModel : childList) {
			if(linkageModel.getLinkage_id()!=-1){
				ids=ids+linkageModel.getLinkage_id()+",";
				this.update(linkageModel);
			}else{
				linkageModel.setLinkage_id(null);
				LinkageModel myLinkageModel=this.insert(linkageModel);
				ids=ids+myLinkageModel.getLinkage_id()+",";
			}
		}
		
		if(ids.length()>0){
			this.linkageDao.deleteByUpdate(ids.substring(0, ids.length()-1),parentLlinkageModel.getLinkage_id());
		}else{
			this.linkageDao.deleteByUpdate("-1",parentLlinkageModel.getLinkage_id());
		}
		this.update(parentLlinkageModel);
		this.adminLog(operParamDto, "更新字典类型[ID:" + 	parentLlinkageModel.getLinkage_id()+"名字："+parentLlinkageModel.getName()+"Code:"+parentLlinkageModel.getCode()+ "]", FrameUtil.getTime(null));
	}
}
